package com.android.hw.projectmenu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class HardwareAgingTest extends ListActivity {
	
	private String TAG = "HardwareAgingTest";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setListAdapter(new SimpleAdapter(this, getData(),
				android.R.layout.simple_list_item_1, new String[] { "title" },
				new int[] { android.R.id.text1 }));
		getListView().setTextFilterEnabled(true);
	}

	
	private List getData() {
		List<Map> dataList = new ArrayList<Map>();

		Intent intent = new Intent(Intent.ACTION_MAIN, null);// 取得标明为Main的activity
		intent.addCategory(Intent.CATEGORY_SAMPLE_CODE);
		PackageManager pManager = getPackageManager();// 获得包名
		List<ResolveInfo> list = pManager.queryIntentActivities(intent, 0);// 装载包路径名

		if (null == list) {

			return dataList;
		}

		int len = list.size();
		Log.i(TAG, String.valueOf(len));

		for (int i = 0; i < len; i++) {
			ResolveInfo info = list.get(i);
			CharSequence lable = info.loadLabel(pManager);

			if ("com.android.hw.projectmenu"
					.equals(info.activityInfo.applicationInfo.packageName)) {
				addItem(dataList,
						lable.toString(),
						myIntent(info.activityInfo.applicationInfo.packageName,
								info.activityInfo.name)); // myIntet()方法设置intent跳转的类名
			}
		}

		return dataList;
	}

	private void addItem(List<Map> data, String name, Intent intent) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("title", name); // 将传入的title放入map中以供填充adapter使用
		map.put("intent", intent);// 将传入的intent放入map中以供跳转使用

		data.add(map);
	}


	private Intent myIntent(String contentPkg, String toPkg) {
		Intent i = new Intent();
		i.setClassName(contentPkg, toPkg);

		return i;

	}


	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Map map = (Map) l.getItemAtPosition(position);

		Intent intent = (Intent) map.get("intent");
		startActivity(intent);

	}

}
